
/*wait主要做三件事：1.解锁  2.阻塞等待  3.当收到通知时就唤醒同时尝试重新获取锁
*  所以wait必须要写到synchronized代码块里面（因为只有先加锁才能解锁）*/
public class ThreadDemo18 {
    public static void main(String[] args) throws InterruptedException {
        System.out.println("notify之前：");
        Object locker = new Object();
        Thread t1 = new Thread(() -> {
            try {
                System.out.println("wait开始 ");
                synchronized (locker) {
                    locker.wait();
                }
                System.out.println("wait结束 ");
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        });
        t1.start();

        Thread.sleep(1000);
        Thread t2 = new Thread(() -> {
            synchronized (locker) {
                locker.notify();
            }
        });
        t2.start();
        System.out.println("notify之后 ");
    }
}
